package sw6.visualschedule;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.util.Pair;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class VerticalScroll extends Activity {

    private LinearLayout mPictureList;
    private ScrollView mScrollView;
    private LinearLayout mButtonBar;
    private boolean stepActive = false;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vertical);
        
        mPictureList = (LinearLayout) findViewById(R.id.picturelist);
        mButtonBar = (LinearLayout) findViewById(R.id.buttonbar);
        mScrollView = (ScrollView) findViewById(R.id.scrollview);
        mScrollView.setSmoothScrollingEnabled(true);
        
        // Disallow manual scrolling in the scrollview 
        mScrollView.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        
        PictureList picList = new PictureList(
        		new ArrayList<Integer>(),
        		new ArrayList<String>()
        );
        
        picList.add(R.drawable.b2toothpasteon, "Stir meget intenst på tandpastaen i 4 minutter.");
        picList.add(R.drawable.b3sidebrush, "børst lidt i siden");
        picList.add(R.drawable.b4frontbrush, "så fortænderne");
        picList.add(R.drawable.b5behindbrush, "Kom godt ind bag alle tændene");
        picList.add(R.drawable.b6flosh, "Husk tandtråd!");
        picList.add(R.drawable.b7nowbrushcatteeth, "Lær katten at børste tænder");
        picList.add(R.drawable.b8smile, "Smil!");
        picList.add(R.drawable.b9, "lollz");
        
        for (Pair<Integer, String> pic : picList) {
        	ImageButton iv = new ImageButton(this);
        	iv.setImageResource(pic.first);
        	
        	// TODO: Only set this on the first item
        	iv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
				    if (stepActive) // another step was already activated, but not finished
                        return;
				    
				    // only allow clicking on the uppermost step in the list. NB: This is a hack.
				    if (mPictureList.getChildAt(0) != v)
				        return;
				    
					v.setEnabled(false);
					ImageButton imageView = ((ImageButton)v);
					ColorMatrix cm = new ColorMatrix();
					cm.setSaturation(0);
					ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
					imageView.setColorFilter(filter);
					
					Button acceptButton = new Button(VerticalScroll.this);
					acceptButton.setText("Done");
					
					acceptButton.setOnClickListener(new OnClickListener() {
                        
                        @Override
                        public void onClick(View v) {                            
                            // remove the content of buttonbar, since we have clicked "Done"
                            mButtonBar.removeAllViews();
                            
                            stepActive = false;
                            
                            mPictureList.removeViewAt(0); // delete pic
                            mPictureList.removeViewAt(0); // delete text
                            mScrollView.smoothScrollTo(0, 0);
                            
                            if (mPictureList.getChildCount() == 0)
                                VerticalScroll.this.finish();
                        }
                    });
					
					mButtonBar.addView(acceptButton);
					stepActive = true;
				}
			});
        	
        	iv.setScaleType(ScaleType.CENTER);

        	mPictureList.addView(iv);
        	if(pic.second != null)
        	{
        		TextView tv = new TextView(this);
        		tv.setText(pic.second);
        		tv.setGravity(Gravity.CENTER);
        		mPictureList.addView(tv);
        	}
		}
		
	}
	
}
